package com.baijia.dao.impl;

import com.baijia.dao.UserRegisterLogDao;
import com.baijia.dao.po.UserLoginLog;
import com.baijia.dao.po.UserRegisterLog;
import com.baijia.util.JdbcUtil;

import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by dudu on 2016/11/3.
 */
@Repository
public class UserRegisterLogDaoImpl extends CommonDaoImpl<UserRegisterLog, Long> implements UserRegisterLogDao {

    public UserRegisterLogDaoImpl() {
        super(UserRegisterLog.class);
    }

    private static String INSERT_SQL = "insert into cdb.user_register_log(user_id, ip, create_time, user_role, client, channel, device, version, channel_id)" +
        "values(?,?,now(),?,?,?,?,?,?)";

    @Override
    public int save(final UserRegisterLog log) {
        return jdbcTemplate.update(INSERT_SQL, new PreparedStatementSetter(){

            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                int index = 0;
                JdbcUtil.setLong(ps, ++index, log.getUserId());
                JdbcUtil.setString(ps, ++index, log.getIp());
                JdbcUtil.setInt(ps, ++index, log.getUserRole());
                JdbcUtil.setInt(ps, ++index, log.getClient());
                JdbcUtil.setString(ps, ++index, log.getChannel());
                JdbcUtil.setString(ps, ++index, log.getDevice());
                JdbcUtil.setString(ps, ++index, log.getVersion());
                JdbcUtil.setString(ps, ++index, log.getChannelId());
            }
        });
    }

    @Override
    protected String selectSql() {
        return null;
    }
}
